package com.token.test.submit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.token.sysflow.action.GetPathApproverAction;
import com.token.sysflow.action.LoadFlowStepAction;
import com.token.sysflow.action.SubmitFlowAction;

import java.io.File;

import com.token.test.util.FuncUtilTest;
import com.token.test.util.IntegrateSqlTest;
import org.apache.commons.io.FileUtils;

/**
 * @author chenjianxiong 2020/4/27
 */
public class SubmitFlow {
    public static void main(String[] args) {
        FuncUtilTest f;
        IntegrateSqlTest iSql;

        try {
            SubmitFlowAction submitFlowAction = new SubmitFlowAction();
            String path = submitFlowAction.getClass().getClassLoader().getResource("json/SubmitFlowAction.json").getPath();
            String json = FileUtils.readFileToString(new File(path), "UTF-8");
            JSONObject jsonObject = JSON.parseObject(json);

            JSONObject tmp;
            JSONObject returnJson;
            String keyUnid;
            iSql = new IntegrateSqlTest();
            iSql.initCon();

            f = new FuncUtilTest(jsonObject.getJSONObject("newLoad"));
            System.out.println("====1.起草进入====");
            new LoadFlowStepAction().executeAction(f, iSql);

            returnJson = f.getJsonData();
            if (!testOk(returnJson)) {
                return;
            }
            System.out.println(" ");
            //==================================================
            tmp = jsonObject.getJSONObject("getPath_01");
            tmp.put("flowId", returnJson.getString("flowId"));
            tmp.put("stepId", returnJson.getString("stepId"));
            f = new FuncUtilTest(tmp);
            System.out.println("====2.提交框====");
            new GetPathApproverAction().executeAction(f, iSql);
            returnJson = f.getJsonData();
            printSelectDiv(returnJson.getJSONArray("data"));
            if (!testOk(returnJson)) {
                return;
            }
            System.out.println(" ");
            //==================================================
            JSONArray data = returnJson.getJSONArray("data");
            JSONObject selectJson = data.getJSONObject(0);

            tmp = jsonObject.getJSONObject("task_01");
            tmp.put("stepId", selectJson.getString("stepId"));
            tmp.put("flowId", selectJson.getString("flowId"));
            tmp.put("selectApprovers", getApprover(selectJson));

            f = new FuncUtilTest(tmp);
            System.out.println("====3.开始提交====");
            new SubmitFlowAction().executeAction(f, iSql);
            returnJson = f.getJsonData();
            if (!testOk(returnJson)) {
                return;
            }
            printNextApproves(returnJson);
            System.out.println(" ");
            //==================================================
            keyUnid = f.getJsonData().getString("keyUnid");
            tmp = jsonObject.getJSONObject("editLoad");
            tmp.put("keyUnid", keyUnid);

            System.out.println("=====回写keyUnid：" + keyUnid);

            f = new FuncUtilTest(tmp);
            System.out.println("====4.编辑进入====");
            new LoadFlowStepAction().executeAction(f, iSql);

            returnJson = f.getJsonData();
            if (!testOk(returnJson)) {
                return;
            }
            System.out.println(" ");
            //==================================================

            tmp = jsonObject.getJSONObject("getPath_02");
            tmp.put("keyUnid", keyUnid);
            f = new FuncUtilTest(tmp);
            System.out.println("====5.提交框====");
            new GetPathApproverAction().executeAction(f, iSql);
            returnJson = f.getJsonData();

            if (!testOk(returnJson)) {
                return;
            }
            printSelectDiv(returnJson.getJSONArray("data"));
            System.out.println(" ");
            //==================================================
            data = returnJson.getJSONArray("data");
            selectJson = data.getJSONObject(0);

            tmp = jsonObject.getJSONObject("task_02");
            tmp.put("stepId", selectJson.getString("stepId"));
            tmp.put("keyUnid", keyUnid);
            tmp.put("selectApprovers", getApprover(selectJson));
            //System.out.println("tmp.toJSONString() = " + tmp.toJSONString());
            System.out.println("====6.提交流程====");
            f = new FuncUtilTest(tmp);
            new SubmitFlowAction().executeAction(f, iSql);
            returnJson = f.getJsonData();
            if (!testOk(returnJson)) {
                return;
            }
            printNextApproves(returnJson);
            System.out.println("returnJson = " + returnJson);
            System.out.println(" ");
            //==================================================
            System.out.println("===========测试完成=========");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     *
     * @param returnJson
     */
    private static void printNextApproves(JSONObject returnJson){
        JSONArray approver = returnJson.getJSONArray("nextApproves");
        StringBuffer blr = new StringBuffer(50);
        blr.append("--->办理人：");
        for(int j=0;j<approver.size();j++){
            if(j>0){
                blr.append(" | ");
            }
            JSONObject appr = approver.getJSONObject(j);
            blr.append(appr.getString("name")).append("-").append(appr.getString("id"));
            if(!appr.getString("auth").equals("")){
                blr.append("被[").append(appr.getString("auth")).append("]授权");
            }

        }
        System.out.println(blr);
    }
    /**
     *
     * @param data
     */
    private static void printSelectDiv(JSONArray data){
        StringBuffer blr;
        for(int i=0;i<data.size();i++){
            blr = new StringBuffer(50);
            JSONObject liuxiang = data.getJSONObject(i);
            JSONArray approver = liuxiang.getJSONArray("approver");
            blr.append("--->流向" ).append(i+1).append(":").append(liuxiang.getString("stepName")).append("(");
            for(int j=0;j<approver.size();j++){
                if(j>0){
                    blr.append(" | ");
                }
                JSONObject appr = approver.getJSONObject(j);
                blr.append(appr.getString("name")).append("-").append(appr.getString("id"));

            }
            System.out.println(blr.append(")"));
        }

    }
    /**
     * @param selectJson
     * @return
     */
    private static String getApprover(JSONObject selectJson) {
        JSONArray approver = selectJson.getJSONArray("approver");
        StringBuilder buf = new StringBuilder(50);
        boolean bb = false;
        for (int i = 0; i < approver.size(); i++) {
            JSONObject o = approver.getJSONObject(i);
            if (o.getBooleanValue("checked")) {
                if (bb) {
                    buf.append(";");
                }
                buf.append(o.getString("id"));
            }
            bb = true;
        }
        return buf.toString();
    }

    /**
     * @param returnJson
     * @return
     */
    private static boolean testOk(JSONObject returnJson) {
        if (returnJson.getIntValue("code") != 0) {
            System.out.println("===遇到错误终止测试===");
            return false;
        } else {
            return true;
        }

    }

}
